#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>

#include "configxml.h"

using namespace Bob;

#define XML_CONFIG_PATH QString("%1/config.xml").arg(QApplication::applicationDirPath())

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    OnInitDialog();
}

MainWindow::~MainWindow()
{
    delete ui;
    ReleaseInstance(m_id);
}

void MainWindow::OnInitDialog()
{
    m_id = CreateInstance();
    LoadFile(m_id,XML_CONFIG_PATH.toLatin1());
    QString name = QString::fromUtf8( ReadStr(m_id,"student/name","Bob"));
    auto age = ReadInt(m_id,"student/age",18);
    auto score = ReadInt(m_id,"student/score",99);
    auto is_male = ReadBool(m_id,"student/ismale",true);
    auto url =QString::fromUtf8(Bob::ReadStr(m_id,"student/url","http://192.168.1.2:16003/api/config/query"));
    qDebug()<<"name: "<<name<<" age: "<<age<<" score: "<<score<<" ismale: "<<is_male<<"url: "<<url<<"\r\n";
    auto str = QString::fromUtf8(Bob::GetDocumentStr(m_id));
    qDebug()<<"document: "<<str<<"\r\n";


    WriteStr(m_id,"student/address","TianJin Of China");
    Bob::WriteInt(m_id,"student/sn",9527);
    Bob::WriteStr(m_id,"student/name","BobLiu");
    Bob::RemoveElement(m_id,"student/score");
    Bob::PrintDocument(m_id);
    SaveFile(m_id,XML_CONFIG_PATH.toLatin1());
}

